import 'package:flutter/material.dart';
import 'package:lottie/lottie.dart';
import 'package:myflutter/config/color_config.dart';

///Create Date:2021/11/24 9:54
///Author: Administrator

class LoadingDialog<T> extends PopupRoute<T> {


  VoidCallback cancelCallback;
  late BuildContext context;
  String? barrierLabel;
  LoadingDialog({Key? key,this.canceledOnTouchOutside=false,required this.cancelCallback,this.barrierLabel});
  ///点击背景是否能够退出
  final bool canceledOnTouchOutside;

  void setBuildContext(BuildContext context){
    this.context=context;
  }

  Widget _dialog(){
    return Material(
      color: Colors.transparent,
      child: Center(
        ///弹框大小
        child: SizedBox(
          width: 120.0,
          height: 120.0,
          child:  Container(
            ///弹框背景和圆角
            decoration: const ShapeDecoration(
              color: Color(0xffffffff),
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.all(
                  Radius.circular(8.0),
                ),
              ),
            ),
            child:Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                Lottie.asset("assets/anim_loading.json",width: 60,height: 60),
                const Padding(
                  padding: EdgeInsets.only(
                    top: 10.0,
                  ),
                  child:  Text(
                    "努力加载中",
                    style:  TextStyle(fontSize: 14.0 ,color: ColorConfig.text333333),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  @override
  Color get barrierColor => Colors.black54;

  @override
  bool get barrierDismissible => canceledOnTouchOutside;

  @override
  Widget buildPage(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation) {
    return _dialog();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Future<RoutePopDisposition> willPop() {
    if(cancelCallback!=null){
      cancelCallback.call();
    }
    return super.willPop();
  }

  @override
  Duration get transitionDuration => const Duration(milliseconds: 150);

  @override
  Widget buildTransitions(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) {
    return _buildMaterialDialogTransitions(context, animation, secondaryAnimation, child);
  }

  Widget _buildMaterialDialogTransitions(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) {
    return FadeTransition(
      opacity: CurvedAnimation(
        parent: animation,
        curve: Curves.easeOut,
      ),
      child: child,
    );
  }

}